import { queryInstalmentWaitPayOrder, addRepayItem, addRepayItemClear, queryBillItemById, queryRepayItemById, waitRepayInstallmentCount } from '../../services/order/api';

export default {
  namespace: 'instalmentWaitPayOrder',

  state: {
    data: [],
    pagination: {
    },
    loading: true,
    operatorLoading: false,
    selectItems: {
      isOverDueSelectItems: [
        { name: '是', value: true },
        { name: '否', value: false },
      ],
    },
    userSelect: [],

    billItemLoading: false,
    repayItemLoading: false,
    currRepayItem: [],
    currBillItem: [],
    
  },

  effects: {
    *fetch({ payload }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(queryInstalmentWaitPayOrder, payload);
      if (response && response.code == 200 && response.data && response.data.records) {
        response.data.records.map(data => {
          data['key'] = data.orderId;
          if (data.billList && data.billList.length > 0) {
            data.billList.map((item, index) => {
              item['key'] = item.intsallmentId;
              return item;
            })
          }
        });
        const r = {
          data: response.data.records,
          pagination: {
            total: response.data.total,
            pageSize: payload.pageSize,
            current: payload.current,
          },
        };
        yield put({
          type: 'save',
          payload: r,
        });
      }

      const countRes = yield call(waitRepayInstallmentCount, {});
      if (countRes && countRes.code == 200 && countRes.data) {
        yield put({
          type: 'saveCount',
          payload: {
            countData: countRes.data,
          }
        });
      }

      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *queryBillItemById({ payload }, { call, put, select }) {
      yield put({
        type: 'changeItemLoading',
        payload: { billItemLoading: true },
      });
      const response = yield call(queryBillItemById, payload);
      if (response && response.code == 200 && response.data && response.data.installmentList) {
        response.data.installmentList.map((item, index) => {
          item['key'] = item.id;
          return item;
        });

        const r = {
          data: response.data.installmentList,
        };
        yield put({
          type: 'saveCurrBillItem',
          payload: r,
        });
      }
      yield put({
        type: 'changeItemLoading',
        payload: { billItemLoading: false },
      });
    },

    *queryRepayItemById({ payload }, { call, put, select }) {
      yield put({
        type: 'changeItemLoading',
        payload: { repayItemLoading: true },
      });
      const response = yield call(queryRepayItemById, payload);
      if (response && response.code == 200 && response.data && response.data.length > 0) {
        response.data.map((item, index) => {
          item['key'] = item.repayItem.id;
          return item;
        });

        const r = {
          data: response.data,
        };
        yield put({
          type: 'saveCurrRepayItem',
          payload: r,
        });
      }
      yield put({
        type: 'changeItemLoading',
        payload: { repayItemLoading: false },
      });
    },

    *addRepayItem({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addRepayItem, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *addRepayItemClear({ payload, callback }, { call, put, select }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addRepayItemClear, payload, );
      if (callback) yield callback(response.code, response.message);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },

    *fetchSelectItems({ payload }, { call, put }) {
      const response = yield call(queryEntity, payload, payload.service, payload.apiUrl);
      if (response && response.code == 200) {
        response.data.records.map(data => data['key'] = data.id);
        const r = {};
        r[payload.selectName] = response.data.records;
        yield put({
          type: 'saveSelectItems',
          payload: r,
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload.data,
        pagination: action.payload.pagination,
      };
    },
    saveCount(state, action) {
      return {
        ...state,
        countData: action.payload.countData
      }
    },
    saveCurrBillItem(state, action) {
      return {
        ...state,
        currBillItem: action.payload.data,
      };
    },
    saveCurrRepayItem(state, action) {
      return {
        ...state,
        currRepayItem: action.payload.data,
      };
    },
    changeItemLoading(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    changeOperatorLoading(state, action) {
      return {
        ...state,
        operatorLoading: action.payload,
      };
    },
    saveSelectItems(state, action) {
      return {
        ...state,
        selectItems: {
          ...state.selectItems,
          ...action.payload,
        },
      }
    },
  },

};
